<template>
  <div>
    <router-view></router-view>
    <div class="container">
      <!--工具栏-->
      <div class="toolbar" style="float:left;padding-top:10px;padding-left:15px;">
        <el-form :inline="true" :model="filters" :size="size">
          <el-form-item>
            <el-input v-model="filters.name" placeholder="用户名"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button icon="fa fa-search" type="primary" @click="findPage(null)">搜索
            </el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="toolbar" style="float:right;padding-top:10px;padding-right:15px;">
        <el-form :inline="true" :size="size">
          <el-form-item>
            <el-button-group>
              <el-tooltip content="刷新" placement="top">
                <el-button icon="fa fa-refresh" @click="findPage(null)">刷新</el-button>
              </el-tooltip>
            </el-button-group>
          </el-form-item>
        </el-form>
      </div>
      <!--表格内容栏-->
      <kt-table :data="pageResult" :columns="filterColumns" :showBatchDelete="EditContor.uesredit"
                opFrom="user"
                @findPage="findPage" @handleEdit="handleEdit" @handleDelete="handleDelete">
      </kt-table>

      <!--新增编辑界面-->
      <el-dialog :title="operation?'新增':'编辑'" width="40%" :visible.sync="dialogVisible" :close-on-click-modal="false">
        <el-form :model="dataForm" label-width="80px" :rules="dataFormRules" ref="dataForm" :size="size"
                 label-position="right">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="dataForm.username" :disabled="true" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="修改密码" prop="password">
            <el-input v-model="dataForm.password" placeholder="输入要修改的密码" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="变更权限" prop="staff">
            <el-input v-model="dataForm.staff" placeholder="输入权限：0（普通用户）1（超级用户）" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="变更活跃" prop="active">
            <el-input v-model="dataForm.active" placeholder="输入权限：0（封禁）1（正常）" auto-complete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button :size="size" @click.native="dialogVisible = false">退出</el-button>
          <el-button :size="size" type="primary" @click.native="submitForm" :loading="editLoading">提交</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
  import KtTable from "@/components/KtTable"
  import {
    format
  } from "@/utils/datetime"

  export default {
    name: 'userPanel',
    components: {
      KtTable,
    },
    props: {
      EditContor: Object
    },
    data() {
      return {
        size: 'medium',
        filters: {
          name: ''
        },
        columns: [],
        filterColumns: [],
        pageRequest: {
          pageNum: 1,
          pageSize: 10
        },
        pageResult: {},
        operation: false, // true:新增, false:编辑
        dialogVisible: false, // 新增编辑界面是否显示
        editLoading: false,
        dataFormRules: {
          capacity: [{
            required: true,
            message: '请输入密钥',
            trigger: 'blur'
          }]
        },
        // 新增编辑界面数据
        dataForm: {
          username: '',
          password: '',
          staff: '',
          active: '',
        },
      }
    }
    ,
    methods: {
      // 获取分页数据
      findPage: function (data) {
        let codepageInfo = {
          page: 1,
          size: 10
        }
        if (data !== null) {
          codepageInfo.page = data.pageRequest.pageNum
        }
        console.log(codepageInfo)
        this.$api.admin.getuserpage(codepageInfo).then((res) => {
          console.log(res.results)
          this.pageResult = res
          // this.codepage.total = res.count
        }).then(data != null ? data.callback : '')
      },
      // 封禁用户
      handleDelete: function (data) {
        let userInfo = {
          username: data.params.username
        }
        this.$api.admin.dodeluser(userInfo).then((res) => {
          if (res.error != null) {
            this.$message({message: res.error, type: 'error'})
          } else {
            data.callback(res)
          }
        }).catch((res) => {
          this.$message({message: res.message, type: 'error'})
        })
      },
      // 显示编辑界面
      handleEdit: function (params) {
        this.dialogVisible = true
        this.operation = false
        // this.dataForm = Object.assign({}, params.row)
        this.dataForm.username = params.row.username
      },
      // 编辑
      submitForm: function () {
        this.$refs.dataForm.validate((valid) => {
          if (valid) {
            this.$confirm('确认提交吗？', '提示', {}).then(() => {
              this.editLoading = true
              let params = Object.assign({}, this.dataForm)
              let userInfo = {
                username: params.username
              }
              if (params.password !== '') {
                userInfo['password'] = params.password
              }
              if (params.staff !== '') {
                userInfo['staff'] = params.staff
              }
              if (params.active !== '') {
                userInfo['active'] = params.active
              }
              console.log(userInfo)

              this.$api.admin.doedituser(userInfo).then((res) => {
                this.editLoading = false
                if (res.error != null) {
                  this.$message({message: res.error, type: 'error'})
                } else if (res.success === 'yes') {
                  this.$message({message: '更改成功！', type: 'success'})
                  this.findPage(null)  //生成成功后刷新页面
                } else {
                  this.$message({message: '更改失败，请稍后再试！', type: 'warning'})
                }
              }).catch((res) => {
                this.$message({message: res.message, type: 'error'})
              })
            })
          } else {
            this.$message({message: '请检查填写是否有误', type: 'warning'})
          }
        })
      },
      // 时间格式化
      dateFormat: function (row, column, cellValue, index) {
        console.log("formating!!")
        console.log(row[column.property])
        return format(row[column.property])
      },
      // 处理表格列过滤显示
      initColumns: function () {
        this.columns = [{
          prop: "name",
          label: "姓名",
          minWidth: 50
        }, {
          prop: "username",
          label: "账号",
          minWidth: 50
        }, {
          prop: "account",
          label: "平安账号",
          minWidth: 100
        }, {
          prop: "days",
          label: "剩余天数",
          minWidth: 60
        }, {
          prop: "exectime",
          label: "执行时间",
          minWidth: 60
        }, {
          prop: "province",
          label: "省",
          minWidth: 50
        }, {
          prop: "city",
          label: "城市",
          minWidth: 50
        }, {
          prop: "area",
          label: "区",
          minWidth: 50,
          // formatter: this.dateFormat
        }, {
          prop: "phone",
          label: "联系电话",
          minWidth: 100
        }, {
          prop: "urname",
          label: "紧急联系人",
          minWidth: 70
        }, {
          prop: "urphone",
          label: "紧急联系电话",
          minWidth: 100
        }, {
          prop: "password",
          label: "密码",
          minWidth: 50
        },
        ]
        this.filterColumns = JSON.parse(JSON.stringify(this.columns));
      }
    },
    mounted() {
      this.initColumns()
    }
  }
</script>

<style scoped lang="scss">
  .container {
    width: 90%;
    -webkit-border-radius: 5px;
    border-radius: 5px;
    -moz-border-radius: 5px;
    background-clip: padding-box;
    margin: 30px auto;
    /*width: 350px;*/
    padding: 35px 35px 45px 35px;
    background: #fff;
    border: 1px solid #eaeaea;
    box-shadow: 0 0 25px #cac6c6;
  }

</style>
